package org.linlinjava.litemall.admin.util;

import com.google.common.collect.Lists;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * List转Page
 */
public class PageUtil {
    public static <T> PageImpl<T> getPageLumierePayMemberUserLogVo(Pageable pageable,  List<T> userInfosList) {
        if (pageable.getOffset() > userInfosList.size()) {
            long total = 0;
            PageImpl<T> emptyPage = new PageImpl<>(Lists.newArrayList(), pageable, total);
            return emptyPage;
        }

        if (pageable.getOffset() <= userInfosList.size() && pageable.getOffset() + pageable.getPageSize() > userInfosList.size()) {
            List<T> ssoUserInfos = userInfosList.subList((int) pageable.getOffset(), userInfosList.size());
            PageImpl<T> ssoUserInfoPage = new PageImpl<>(ssoUserInfos, pageable, userInfosList.size());
            return ssoUserInfoPage;
        }
        List<T> ssoUserInfos = userInfosList.subList((int) pageable.getOffset(), (int) (pageable.getOffset() + pageable.getPageSize()));
        PageImpl<T> ssoUserInfoPage = new PageImpl<>(ssoUserInfos, pageable, userInfosList.size());
        return ssoUserInfoPage;
    }

}
